//
//  HomeViewController.m
//  Inforcollection
//
//  Created by macbook on 15/12/1.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "HomeViewController.h"
#import "announcementVC.h"
#import "JCTopic.h"
#import "AnnounTableCell.h"
#import "UtilObject.h"
#import "announModel.h"

static NSString *OneID=@"OnePicNewsTableViewCell";

static NSString *SecondID=@"SecondSectionTableViewCell";

#define KTitleFont [UIFont systemFontOfSize:15]

@interface HomeViewController ()
{

    NSArray*announcementarray;
    UIView *noDataView;
    NSArray*isdatenill;
    UITableView*MagersTable;
    NSInteger pageIndex;//当前页数
    NSMutableArray*MagersData;
    BOOL headRefresh;
}
@end

@implementation HomeViewController
{
    
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    isdatenill=[NSArray array];
    MagersData=[NSMutableArray array];
    pageIndex=1;
    headRefresh=YES;
    self.view.backgroundColor=RGB_AppWhite;
    [self setNaviBarTitle:@"公告管理"];
    [self setNavBarColor:APPNavibackColor];
    [self headerRereshing];
    [self initView];
}

-(void)initView
{
    MagersTable=[[UITableView alloc]initWithFrame:CGRectMake(0,APPNaviHeight, ScreenWidth, ScreenHeight-APPNaviHeight-40-50) style:UITableViewStylePlain];
    MagersTable.delegate=self;
    MagersTable.dataSource=self;
    MagersTable.showsVerticalScrollIndicator=NO;
    MagersTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self setupRefresh:MagersTable type:2];
    
    [self.view addSubview:MagersTable];
    noDataView = [UtilObject getNodataTipView];
    [MagersTable addSubview:noDataView];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return MagersData.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnnounTableCell*cell=[[[NSBundle mainBundle]loadNibNamed:@"AnnounTableCell" owner:nil options:nil]firstObject];
    NSDictionary*dataDic=[NSDictionary dictionary];
    if(MagersData.count !=0){
        
    dataDic=MagersData[indexPath.section];
    cell.DetailFileds.text=dataDic[@"title"];
    
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    announcementVC*annicount=[[announcementVC alloc]init];
    announModel*annomodel=[announModel UserModelWithDic:MagersData[indexPath.section]];
    NSArray*ModeArray=@[annomodel.title,annomodel.publishUser,annomodel.reveiveBranchName,annomodel.publishTime,annomodel.content];
    annicount.reveiveArray=ModeArray;
    if ([annomodel.isChecked intValue]==1) {
        annicount.isChecked=YES;

    }else{
        annicount.isChecked=NO;
    }
    annicount.noticeReveiveRecordId=annomodel.noticeReveiveRecordId;
    [annicount returnText:^(NSString *data) {
        [self headerRereshing];
    }];
   [self.navigationController pushViewController:annicount animated:YES];
}

//公告管理列表
-(void)getAnnouncementDataforUser{
    
    NSMutableDictionary*info=[NSMutableDictionary dictionary];
    [info setValue:[UserData shareUserData].userModel.dbId forKey:@"reveiverId"];
    [info setValue:[NSString stringWithFormat:@"%li",(long)pageIndex] forKey:@"pageIndex"];
    [info setValue:@"15" forKey:@"pageSize"];
    [MBProgressHUD showMessag:@"正在查询..." toView:SharedApplication.keyWindow ];
    
    [[HttpAPI shareHttpAPI]getAnnouncementData:info CallBack:^(NSDictionary *receivedData) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [MBProgressHUD hideAllHUDsForView:SharedApplication.keyWindow animated:YES];
        NSDictionary*DictV=[NSDictionary dictionary];
        if ([receivedData[@"code"]integerValue]==200) {
        [MBProgressHUD showSuccess:receivedData[@"desc"] toView:self.view];
            DictV=receivedData[@"data"];
            isdatenill=DictV[@"data"];
            [MagersData addObjectsFromArray:isdatenill];
            if (MagersData.count==0)
            {
                [MagersTable addSubview:noDataView];
            }else
            {
                [noDataView removeFromSuperview];
            }
            [MagersTable reloadData];
            
        }else{
            [MBProgressHUD showError:@"网络错误" toView:self.view];
        }
        [MagersTable headerEndRefreshing];
        [MagersTable footerEndRefreshing];
    }];
    
}
-(void)viewDidAppear:(BOOL)animated{
    
   [MagersTable reloadData];
    
}

#pragma mark - refresh data
-(void)headerRereshing
{
    [MagersData removeAllObjects];
    
    pageIndex=1;
    [self getAnnouncementDataforUser];
   
}

-(void)footerRereshing
{
    pageIndex=pageIndex+1;
    [self getAnnouncementDataforUser];
   
}

@end
